package cn.kevinkun.joystick;

import java.io.IOException;

import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.SimpleEvent;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.annotations.SimpleProperty;
import com.google.appinventor.components.annotations.UsesPermissions;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.Component;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;
import com.google.appinventor.components.runtime.Form;
import com.google.appinventor.components.runtime.HVArrangement;
import com.google.appinventor.components.runtime.errors.YailRuntimeError;
import com.google.appinventor.components.runtime.util.MediaUtil;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.ViewGroup;
import cn.kevinkun.joystick.JoystickView.OnMoveListener;

@DesignerComponent(version = 1, description = "joystick extension writen by Kevinkun (wangsk789@qq.com), ", category = ComponentCategory.EXTENSION, nonVisible = true, iconName = "images/extension.png", helpUrl = "https://kevinkun.cn/joystick")
@SimpleObject(external = true)
@UsesPermissions(permissionNames = "")
// @UsesLibraries(libraries = "joystick.jar")
public class JoyStick extends AndroidNonvisibleComponent implements Component {
	final String LOG_TAG = "JoyStick";
	Context context;
	Form form;
	JoystickView joystick;
	int interval = 50;
	int backgroundColor = Color.LTGRAY;
	int buttonColor = Color.BLACK;

	public JoyStick(ComponentContainer container) {
		super(container.$form());
		context = container.$context();
		form = container.$form();
	}

	OnMoveListener listener = new OnMoveListener() {

		@Override
		public void onMove(int angle, int strength) {
			Dragged(angle, strength);
		}
	};

	@SimpleFunction(description = "where the joystick to be shown")
	public void Init(HVArrangement layout) {
		ViewGroup view = (ViewGroup) layout.getView();
		// view.removeAllViews();

		joystick = new JoystickView(context);
		view.addView(joystick, -1, -1);
		joystick.setOnMoveListener(listener, interval);
	}

	@SimpleEvent(description = "fired when joystick is touched and dragged")
	public void Dragged(int angle, int strength) {
		EventDispatcher.dispatchEvent(this, "Dragged", angle, strength);
	}

	@SimpleProperty(description = "interval (in milliseconds) to fire OnMove event. default to 50")
	public void Interval(int interval) {
		this.interval = interval;
		joystick.setOnMoveListener(listener, interval);
	}

	// @SimpleProperty
	// public int Interval() {
	// return interval;
	// }

	@SimpleProperty(description = "background color. default to lightgray")
	public void BackgroundColor(int color) {
		this.backgroundColor = color;
		joystick.setBackgroundColor(color);
	}

	// @SimpleProperty
	// public int BackgroundColor() {
	// return backgroundColor;
	// }
	@Deprecated
	@SimpleProperty(description = "background image. default to null")
	public void BackgroundImage(String image) {
		Drawable d;
		try {
			// d = new BitmapDrawable(BitmapFactory.decodeStream(openFile(image)));
			d = MediaUtil.getBitmapDrawable(this.form, image);
			joystick.setBackgroundDrawable(d);
		} catch (IOException e) {
			Log.e(LOG_TAG, e.getMessage());
			throw new YailRuntimeError(e.getMessage(), LOG_TAG);
		}
	}

	@SimpleProperty(description = "button color. default to dark")
	public void ButtonColor(int color) {
		this.buttonColor = color;
		joystick.setButtonColor(color);
	}

	// @SimpleProperty
	// public int ButtonColor() {
	// return buttonColor;
	// }

	@SimpleProperty(description = "button image. default to null")
	public void ButtonImage(String image) {
		Drawable d;
		try {
			// d = new BitmapDrawable(BitmapFactory.decodeStream(openFile(image)));
			d = MediaUtil.getBitmapDrawable(this.form, image);
			joystick.setButtonDrawable(d);
		} catch (IOException e) {
			Log.e(LOG_TAG, e.getMessage());
			throw new YailRuntimeError(e.getMessage(), LOG_TAG);
		}
	}

	@SimpleProperty(description = "ratio (0 < ratio < 1) of background to its container. default to 0.75f")
	public void BackgroundSizeRatio(float ratio) {
		joystick.setBackgroundSizeRatio(ratio);
	}

	// @SimpleProperty
	// public float BackgroundSizeRatio() {
	// return joystick.getBackgroundSizeRatio();
	// }

	@SimpleProperty(description = "ratio (0 < ratio < 1) of button to its container. default to 0.25f")
	public void ButtonSizeRatio(float ratio) {
		joystick.setButtonSizeRatio(ratio);
	}

	// @SimpleProperty
	// public float ButtonSizeRatio() {
	// return joystick.getButtonSizeRatio();
	// }

	@SimpleProperty(description = "if the button will be set to center when not touching. default to true")
	public void AutoReCenterButton(boolean auto) {
		joystick.setAutoReCenterButton(auto);
	}

	// @SimpleProperty
	// public boolean AutoReCenterButton() {
	// return joystick.isAutoReCenterButton();
	// }

	@SimpleProperty(description = "if the joystick is enabled. default to true")
	public void Enabled(boolean b) {
		joystick.setEnabled(b);
	}

	// @SimpleProperty
	// public boolean Enabled() {
	// return joystick.isEnabled();
	// }

	@SimpleProperty(description = "direction of the button moving. \ndefault to 0\nany negative value (e.g. -1) for the horizontal axe\n"
			+ "any positive value (e.g. 1) for the vertical axe\n" + "zero (0) for both (which is the default option)")
	public void ButtonDirection(int d) {
		joystick.setButtonDirection(d);
	}

	// @SimpleProperty
	// public int ButtonDirection() {
	// return joystick.getButtonDirection();
	// }

	@SimpleProperty(description = "if the button will stick to border. default to false")
	public void ButtonStickToBorder(boolean b) {
		joystick.setButtonStickToBorder(b);
	}

}
